package com.andromeda.tutorial;

import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;

public class DragSurface extends View{

	private int borderTop;
	private int borderLeft;
	private int borderRight;
	private int borderDown;
	private int center_x;
	private int center_y;
	private int spaceX1;
	private int spaceX2;
	private int spaceY;
	private int item_index;
	private int iTem_radius;
	private int touchAndDrag_radius;
	private int i,k;
	
	
	private boolean is_facebook_active;
	private boolean is_twitter_active;
	private boolean is_finger_on_camera;
	
	private iTem camera;
	private iTem[] iTems;
	
	
	//private TwitterPost tp;
	//private File imgFile;
	
	public DragSurface(Context context,int screenHeight,int screenWidth){
		super(context);
		setFocusable(true);
		
		this.is_facebook_active=false;
		this.is_twitter_active=false;
		this.is_finger_on_camera=false;
		
		this.item_index=-1;
		this.i=0;
		this.k=0;
		
		this.center_x=screenWidth/2;
		this.center_y=screenHeight/2-(int) (screenHeight/9.7);
		this.camera=new iTem(context, R.drawable.touchanddrag,R.drawable.cameraa,R.drawable.camerap, center_x,center_y,0);
		this.touchAndDrag_radius=camera.getImg().getHeight()/2;
        this.iTem_radius=camera.getImgShape().getHeight()/2;
	
        this.iTems=new iTem[6];
		this.spaceX1=(int) (screenWidth/4.55);
		this.spaceX2=(int) (screenWidth/3.4);
		this.spaceY=(int) (screenHeight/4.27);
		
		iTems[0]=new iTem(context, R.drawable.iloveexpo,R.drawable.iloveshape,R.drawable.iloveselected,center_x-spaceX1,center_y-spaceY,6);
		iTems[1]=new iTem(context, R.drawable.ibuyexpo,R.drawable.ibuyshape,R.drawable.ibuyselected,center_x+spaceX2,center_y,4);
		iTems[2]=new iTem(context, R.drawable.ishockexpo,R.drawable.ishockshape,R.drawable.ishockselected,center_x-spaceX2,center_y,5);
		iTems[3]=new iTem(context, R.drawable.ihateexpo,R.drawable.ihateshape,R.drawable.ihateselected,center_x+spaceX1,center_y+spaceY,1);
		iTems[4]=new iTem(context, R.drawable.ilikeexpo,R.drawable.ilikeshape,R.drawable.ilikeselected,center_x-spaceX1,center_y+spaceY,8);
		iTems[5]=new iTem(context, R.drawable.ineedexpo,R.drawable.ineedshape,R.drawable.ineedselected,center_x+spaceX1,center_y-spaceY,2);
		
		
	}
	
	@Override
	protected void onDraw(Canvas canvas) {                                               
		  
		  for (iTem item : iTems) {
	            canvas.drawBitmap(item.getImg(), item.getCoordX(), item.getCoordY(), null);  
	        }
		  canvas.drawBitmap(camera.getImg(), camera.getCoordX(), camera.getCoordY(), null);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		if(k==0){
			int height=this.getHeight();
			int width=this.getWidth();
			this.borderTop=10;
			this.borderDown=height-10;
			this.borderLeft=10;
			this.borderRight=width-10;
			this.k=1;
		}
		
		if(k==1){
			
			int eventAction=event.getAction();
			int current_x=(int)event.getX();
			int current_y=(int)event.getY();
			
			switch(eventAction){
			
			case MotionEvent.ACTION_DOWN : 
				if(Math.sqrt( (double) (((center_x-current_x)*(center_x-current_x)) + (center_y-current_y)*(center_y-current_y))) <= touchAndDrag_radius){
					camera.setCoordX(current_x-iTem_radius);
					camera.setCoordY(current_y-iTem_radius);
					camera.turnToShape();
					is_finger_on_camera=true;
					
					iTems[0].turnToShape();
					iTems[1].turnToShape();
					iTems[2].turnToShape();
					iTems[3].turnToShape();
					iTems[4].turnToShape();
					iTems[5].turnToShape();
				}
				break;
			       
			case MotionEvent.ACTION_MOVE :
				
				if(is_finger_on_camera){
					if(current_x-iTem_radius>=borderLeft && current_x+iTem_radius<=borderRight){
						camera.setCoordX(current_x-iTem_radius);
					}
					if(current_y-iTem_radius>=borderTop && current_y+iTem_radius<=borderDown){
						camera.setCoordY(current_y-iTem_radius);
					}
				
					item_index=-1;
					
					if(Math.sqrt( (double) (((iTems[0].getCenter_x()-current_x)*(iTems[0].getCenter_x()-current_x)) + (iTems[0].getCenter_y()-current_y)*(iTems[0].getCenter_y()-current_y))) <= 2*iTem_radius){
						item_index=0;
					}
					
					else if(Math.sqrt( (double) (((iTems[1].getCenter_x()-current_x)*(iTems[1].getCenter_x()-current_x)) + (iTems[1].getCenter_y()-current_y)*(iTems[1].getCenter_y()-current_y))) <= 2*iTem_radius){
						item_index=1;
					}
					
					else if(Math.sqrt( (double) (((iTems[2].getCenter_x()-current_x)*(iTems[2].getCenter_x()-current_x)) + (iTems[2].getCenter_y()-current_y)*(iTems[2].getCenter_y()-current_y))) <= 2*iTem_radius){
						item_index=2;
					}
					
					else if(Math.sqrt( (double) (((iTems[3].getCenter_x()-current_x)*(iTems[3].getCenter_x()-current_x)) + (iTems[3].getCenter_y()-current_y)*(iTems[3].getCenter_y()-current_y))) <= 2*iTem_radius){
						item_index=3;
					}
					
					else if(Math.sqrt( (double) (((iTems[4].getCenter_x()-current_x)*(iTems[4].getCenter_x()-current_x)) + (iTems[4].getCenter_y()-current_y)*(iTems[4].getCenter_y()-current_y))) <= 2*iTem_radius){
						item_index=4;
					}
					
					else if(Math.sqrt( (double) (((iTems[5].getCenter_x()-current_x)*(iTems[5].getCenter_x()-current_x)) + (iTems[5].getCenter_y()-current_y)*(iTems[5].getCenter_y()-current_y))) <= 2*iTem_radius){
						item_index=5;
					}
					
					
				
					if(item_index==-1){
						
						iTems[0].shake();
						iTems[1].shake();
						iTems[2].shake();
						iTems[3].shake();
						iTems[4].shake();
						iTems[5].shake();
						
						if(i==1){
							iTems[0].turnToShape();
							iTems[1].turnToShape();
							iTems[2].turnToShape();
							iTems[3].turnToShape();
							iTems[4].turnToShape();
							iTems[5].turnToShape();
							camera.turnToShape(0);
							i=0;
						}
						   
					}
					
					else{
						if(i==0){
						  iTems[item_index].turnToSelected();
						  camera.turnToSelected();
						  i=1;
						}
					}
					
				}
				break;
				
			case MotionEvent.ACTION_UP :  
				camera.turnToExpo();
				iTems[0].turnToExpo();
				iTems[1].turnToExpo();
				iTems[2].turnToExpo();
				iTems[3].turnToExpo();
				iTems[4].turnToExpo();
				iTems[5].turnToExpo();
				
				is_finger_on_camera=false;
				
				break;
			}
			
			invalidate();	
	    }
		return true;
	}
	
	public boolean isFacebookActive(){
		return is_facebook_active;
	}
	
	public boolean isTwitterActive(){
		return is_twitter_active;
	}
	
	public void changeFacebookStatus(){
		if(is_facebook_active){
			is_facebook_active=false;
		}
		else{
			is_facebook_active=true;
		}
	}
	
	public void changeTwitterStatus(){
		if(is_twitter_active){
			is_twitter_active=false;
		}
		else{
			is_twitter_active=true;
		}
	}
	
}

